// 获取url中的参数
function getUrlParam (name) {
     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if (r!= null) {
        return unescape(r[2]);
     }else{
        return null;
     }
}
//写cookies
function setCookie(name,value)
{
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+';path=/';
}
//读取cookies
function getCookie(name)
{
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg))
		return unescape(arr[2]);
	else
		return null;
}
//删除cookies
function delCookie(name)
{
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval=getCookie(name);
	if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString()+';path=/';
}
//清除所有的cookies
function clearCookie(){ 
	var keys=document.cookie.match(/[^ =;]+(?=\=)/g); 
		if (keys) { 
		for (var i = keys.length; i--;) 
		document.cookie=keys[i]+'=0;expires=' + new Date( 0).toUTCString() 
	}
}
//随机数
var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
function generateMixed(n) {
     var res = "";
     for(var i = 0; i < n ; i ++) {
         var id = Math.ceil(Math.random()*35);
         res += chars[id];
     }
     return res;
}
// 清除两边的空格 
String.prototype.trim = function() { 
  return this.replace(/(^\s*)|(\s*$)/g, ''); 
}; 
// 合并多个空白为一个空白 
String.prototype.ResetBlank = function() { 
  var regEx = /\s+/g; 
  return this.replace(regEx, ' '); 
}; 
 
// 保留数字 
String.prototype.GetNum = function() { 
  var regEx = /[^\d]/g; 
  return this.replace(regEx, ''); 
}; 
 
// 保留中文 
String.prototype.GetCN = function() { 
  var regEx = /[^\u4e00-\u9fa5\uf900-\ufa2d]/g; 
  return this.replace(regEx, ''); 
}; 
 
// String转化为Number 
String.prototype.ToInt = function() { 
  return isNaN(parseInt(this)) ? this.toString() : parseInt(this); 
}; 
 
// 得到字节长度 
String.prototype.GetLen = function() { 
  var regEx = /^[\u4e00-\u9fa5\uf900-\ufa2d]+$/; 
  if (regEx.test(this)) { 
    return this.length * 2; 
  } else { 
    var oMatches = this.match(/[\x00-\xff]/g); 
    var oLength = this.length * 2 - oMatches.length; 
    return oLength; 
  } 
};
//取得用户信息
function getToKen(){
	var _json = {
		"username" : "guest_" + generateMixed(8),
		"token" : "",
		"usertype" : 0,
		"userid" : 0,
	};
	var userinfo = getCookie("user");
	if(userinfo){
		var _user = eval("("+userinfo+")");
		_json = {
			"userid" : _user['id'],
			"usertype" : _user['usertype'],
			"username" : _user['username'],
			"token" : _user['token'],
		};
	}
	return _json;
}
//红包倒计时
//定义一个30秒计时器变量
var countdown=30; 
//构造一个倒计时函数叫settime
function hb_settime(obj_a) { 
    //开始判断倒计时是否为0
    if (countdown == 0) { 
        obj_a.setAttribute("disabled", true);
        countdown = 30;
        obj_a.classList.add("mui-disabled");
        obj_a.querySelector(".hb_times").innerHTML = '已结束';
        //立即跳出settime函数，不再执行函数后边的步骤
        return;
    } else {
    	obj_a.querySelector("em").innerHTML = countdown;
        countdown--;
    } 
   //过1秒后执行倒计时函数
   setTimeout(function() {hb_settime(obj_a)},1000)
}
//红包游戏倒计时
var game_OverTime = 180;
function game_setTime(objID){
	//开始判断倒计时是否为0
    if (game_OverTime == 0) { 
        //game_OverTime = overTime;
        //立即连接服务器取计算结算
        
        //立即跳出settime函数，不再执行函数后边的步骤
        return false;
    } else {
    	objID.querySelector("em").innerHTML = game_OverTime;
        game_OverTime--;
        //过1秒后执行倒计时函数
   		setTimeout(function(){
   			game_setTime(objID);
   			},1000);
    } 
}
//打开聊天室窗口
function openChatWindow(tourl,room_id,name,roomname){
	if(name === "null"){
		name = "游客";
	}
	mui.openWindow({
		url : '../room/room-base.html',
		id : "room/room-base.html",
		show:{                               
            aniShow:"slide-in-right",
            duration:300,
            autoShow:true
        },
         extras:{
         	room_id : room_id,
         	room_name : roomname,
         	username : name,
         },
	});
}
//打开登录窗口
function openLoginWindow(room_id,notieWebview){
	isDebug && console.log("从房间号开启登录口："+room_id);
	mui.openWindow({
		url : "../user/login.html",
		id : "user/login.html",
		show:{                               
            aniShow:"slide-in-right",
            duration:300,
            autoShow:true
       },
        extras:{
        	room_id : room_id,
        	pWebview : notieWebview,
        },
	});
}
//检测返回数据状态码
function exitsState(data){
	if(data.state != 200){
    	mui.toast(info[data.msgid]);
    	return false;
    }
}
//信息提示
function infoSay(obj,stringContent){
	var li = document.createElement("li");
	li.className = "game-chat-li game-chat-msg";
	li.innerHTML = stringContent;
	obj.appendChild(li);
}
//错误提示
function errorSay(obj,stringContent){
	var li = document.createElement("li");
	li.className = "game-chat-li game-chat-error";
	li.innerHTML = stringContent;
	obj.appendChild(li);
}
//抢到红包的提示
function getHbSay(obj,data){
	var li = document.createElement("li");
	li.className = "game-chat-li game-chat-hb";
	li.innerHTML = '<img src="../imgs/hb-icon-256-256.png" /><p>游客抢到了红包1.34</p>';
	obj.appendChild(li);
}
//别人说话样式
function sheSay(obj,data){
	var li = document.createElement("li");
	li.className = "game-chat-li";
	li.innerHTML = '<div class="game-chat-user">\
							<img src="../imgs/chatroom-no-logo.png">\
							<cite>'+data.name+' <span>'+data.time+'</span></cite>\
						</div>\
						<div class="game-chat-text">'+data.content+'</div>';
	obj.appendChild(li);
}
// 自己说话
function meSay(obj,data){
	//from_client_id, from_client_name, content, time
	var chat_li = document.createElement('li');
		chat_li.className = 'game-chat-li game-chat-mine';
		chat_li.innerHTML = '<div class="game-chat-user">\
								<img src="../imgs/chatroom-no-logo.png">\
								<cite><span>'+data.time+'</span> '+data.name+'</cite>\
							</div>\
							<div class="game-chat-text">'+data.content+'</div>';
	obj.appendChild(chat_li);
}
